package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.PunchClockArticleInfoMapper;
import com.tulele.vtool.mapper.UserPunchClockInfoMapper;
import com.tulele.vtool.model.PunchClockArticleInfo;
import com.tulele.vtool.model.UserPunchClockInfo;
import com.tulele.vtool.service.IUserPunchClockInfoService;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.ToolUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class UserPunchClockInfoServiceImpl implements IUserPunchClockInfoService {

    @Autowired
    private UserPunchClockInfoMapper userPunchClockInfoMapper;

    @Autowired
    private PunchClockArticleInfoMapper punchClockArticleInfoMapper;

    @Override
    public int userPunchClockProcess(Integer userId, Integer clockArticleId, Date clockTime) {

        //查询用户是否当天已经打卡
        String clockTimeStr = DateUtil.getFormatDate("yyyy-MM-dd", clockTime);// 打卡文章所属时间(clock_article_time)对应的日期
        String curDate = DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate());// 今天的日期
        // 如果打卡文章所属时间(clock_article_time)是今天，再判断用户今天是否打卡。即今天以前的打卡文章，不能再次打卡
        if (clockTimeStr.equals(curDate)) {
            int clockStatus = Optional.ofNullable(userPunchClockInfoMapper.queryUserSpecialDayPunchClockStatus(userId, clockTimeStr)).orElse(0);
            if (clockStatus == 0) {

                UserPunchClockInfo userPunchClockInfo = new UserPunchClockInfo();
                userPunchClockInfo.setUserId(userId);
                userPunchClockInfo.setClockArticleId(clockArticleId);
                userPunchClockInfo.setCreateTime(DateUtil.getCurDate());

                userPunchClockInfoMapper.addUserPunchClockInfo(userPunchClockInfo);

            }
        }

        //查询累计的打卡天数
//        int clockTimes = Optional.ofNullable(userPunchClockInfoMapper.queryUserPunchClockDays(userId)).orElse(0);

        // 确认打卡截止日期
        String endDate = clockTimeStr + " 23:59:59";
        // 查询到截止日期的打卡次数
        int clockTimes = Optional.ofNullable(userPunchClockInfoMapper.queryUserPunchClockDaysByDate(userId, endDate)).orElse(0);

        return clockTimes;
    }

    @Override
    public PunchClockArticleInfo getUserPunchClockArticleInfo() {
        //true-上午 false-下午
        boolean resultStatus = ToolUtil.getAmPmTimePeriodStatus();

        int clockArticleTimeType = 1;
        if (!resultStatus) {
            clockArticleTimeType = 2;
        }

        PunchClockArticleInfo punchClockArticleInfo=punchClockArticleInfoMapper.queryCurDayPunchClockArticleInfo(clockArticleTimeType);
        if(punchClockArticleInfo==null){//如果为空就随机获取一条
            punchClockArticleInfo= punchClockArticleInfoMapper.queryRANDPunchClockArticleInfo(clockArticleTimeType);
        }

        return punchClockArticleInfo;
    }

    @Override
    public PunchClockArticleInfo getPunchClockArticleInfoById(Integer clockArticleId) {
        return punchClockArticleInfoMapper.queryPunchClockArticleInfoById(clockArticleId);
    }

    @Override
    public Integer getTotalUserPunchClockDays() {
        return userPunchClockInfoMapper.queryTotalUserPunchClockDays();
    }

    @Override
    public int getUserClockDays(Integer userId) {

        //查询累计的打卡天数
        int clockTimes = Optional.ofNullable(userPunchClockInfoMapper.queryUserPunchClockDays(userId)).orElse(0);

        return clockTimes;
    }

    /**
     * 查询用户当天是否有打卡
     * @param userId
     * @return
     */
    @Override
    public Integer queryUserSpecialDayPunchClockStatus(Integer userId, String clockTime){
        return userPunchClockInfoMapper.queryUserSpecialDayPunchClockStatus(userId,clockTime);
    }
}
